﻿/*
 * Created by SharpDevelop.
 * User: hugo
 * Date: 2012/11/26
 * Time: 下午 08:45
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using Microsoft.Practices.ServiceLocation;
using MyPetShop.Domain.Exception;
using NCommon.Data;
using NCommon.Events;
using NCommon.Rules;

namespace MyPetShop.Domain
{
	/// <summary>
	/// Description of OrderService.
	/// </summary>
	public class OrderService
	{
		public void CheckOut(Shopping shopping)
		{
		    var validator = ServiceLocator.Current.GetInstance<IValidationRule<ShoppingDetail>>("OverStockValidate");

		    foreach (var details in shopping.ShoppingDetails)
		    {
		        if(validator.Validate((details)) == false)
		        {
                    throw new OverStockException();
		        }
		    }

		    var order = OrderFactory.MakeOrder(shopping);

		    var orderRepository = ServiceLocator.Current.GetInstance<IRepository<Order>>();
            orderRepository.Add(order);

            DomainEvent.Raise(new OrderCreateEvent(order));
		}
	}
}
